<?php if (!defined("BASEPATH")) exit("No direct script access allowed");

class Mapper extends MY_Controller {

	function __construct() {
		parent::__construct();
	}
	
	function index() {
		$params["getUnmappedElements"] = $this->odds_model->getUnmappedElements();
		$params["page_header"] = "Mapper (<span id=\"mapperCnt\">".count($params["getUnmappedElements"])."</span>)";
		$params["getArbs"] = $this->odds_model->getArbs();
		$params["content"] = $this->load->view("mapper/index", $params, TRUE);
		$this->load->view("template/page", $params);
	}
	
	function search() {
		$this->load->model("mapping_model");
		$where = "";
		$searchval = $this->input->post("searchval");
		if(strpos(" ", $searchval) !== FALSE) {
			$searchval = preg_split("/ /", $searchval);
			foreach($searchval as $val) {
				$where .= (($where)?(" or "):("")) . " map_group_name like '%".$this->db->escape_str($val)."%'";
			}
		} else {
			$searchval = preg_split("/\+/", $searchval);
			foreach($searchval as $val) {
				$where .= (($where)?(" and "):("")) . "map_group_name like '%".$this->db->escape_str($val)."%'";
			}
		}
		$params["searchval"] = $searchval;
		$params["searchMappedElement"] = $this->mapping_model->searchMappedElement($where);
		$this->load->view("mapper/search", $params);
	}
	
	function save() {
		$this->load->model("mapping_model");
		if($this->input->post("unmappedElement") and $this->input->post("mapElement")) {
			$this->mapping_model->insert(array(
				"map_group_name" => $this->input->post("mapElement"),
				"map_child_name" => $this->input->post("unmappedElement"),
			));
			$this->odds_model->mapping();
		}
	}
	
}